import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

// 恐龙类
public class Dinosaur {
    public BufferedImage image; // 主图片
    protected BufferedImage image1, image2, image3; // 跑步图片
    public int x, y; // 坐标
    protected int jumpValue = 0; // 跳跃的增变量
    protected boolean jumpState = false; // 跳跃状态
    protected boolean downState = false;
    protected int stepTimer = 0; // 踏步计时器
    protected final int LOWEST_Y = 120; // 落地最低坐标

    public Dinosaur() {
        x = 50;         // 恐龙的横坐标
        y = LOWEST_Y;   // 恐龙的纵坐标
        try {
            image1 = ImageIO.read(new File("image/k1.png")); // 读取恐龙的图片
            image2 = ImageIO.read(new File("image/k2.png"));
            image3 = ImageIO.read(new File("image/k3.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 踏步
    protected void step() {
        stepTimer += FreshThread.fresh; // 计时器递增
        if (downState) {
            image = image3;
            return;
        }
        int tmp = stepTimer / 150 % 3;
        if (tmp == 1) {
            image = image1;
        } else {
            image = image2;
        }
    }

    // 跳跃
    protected void jump() {
        if (!jumpState) { // 如果没处于跳跃状态
            Sound.jump(); // 播放跳跃音效
        }
        jumpState = true; // 处于跳跃状态
    }

    protected void down() {
        if (jumpState) return;
        downState = true;
    }

    public boolean getDownState() {
        return downState;
    }

    protected void up() {
        if (jumpState) return;
        downState = false;
        y = LOWEST_Y;
    }

    // 移动
    protected void move() {
        step(); // 不断踏步
        if (jumpState) { // 如果正在跳跃
            if (y == LOWEST_Y) { // 如果纵坐标大于等于最低点---（越往上坐标越小）
                jumpValue = -5; // 增变量为负值--向上跳
            }
            if (y <= 0) {// 如果跳过最高点
                jumpValue = 6; // 增变量为正值--向下跳
            }
            y += jumpValue; // 纵坐标发生变化
            if (y >= LOWEST_Y) { // 如果再次落地
                y = LOWEST_Y;
                jumpState = false; // 停止跳跃
            }
        } else if (downState){
            y = LOWEST_Y + 40;
        }
    }

    public Rectangle getFootBounds() { // 获取恐龙的脚部边界对象
        if (downState) return new Rectangle(x + 10, y + 10, 70, 25);
        return new Rectangle(x + 30, y + 49, 29, 28); // 用于后续做碰撞检测
    }

    public Rectangle getHeadBounds() { // 获取恐龙的头部边界对象
        return new Rectangle(x + 46, y + 5, 32, 22);
    }
}